package com.easy.life.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.life.common.R;
import com.easy.life.dao.ImageDao;
import com.easy.life.entity.ImageEntity;
import com.easy.life.po.RoomInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.easy.life.dao.RoomDao;
import com.easy.life.entity.RoomEntity;
import com.easy.life.service.RoomService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomDao, RoomEntity> implements RoomService {

    @Resource
    private RoomDao roomDao;
    @Resource
    private ImageDao imageDao;

    @Override
    public R searchRoom(String city, Integer type,Long userId) {
        List<RoomEntity> rooms = roomDao.selectList(new QueryWrapper<RoomEntity>().like("city", city)
                .eq("type", type).eq("status",1).ne("userId",userId));
        return R.ok().put("data",rooms);
    }

    @Override
    public R publishRoom(RoomInfo roomInfo) {
        RoomEntity room=new RoomEntity();
        room.setAddress(roomInfo.getAddress());
        room.setArea(roomInfo.getArea());
        room.setCity(roomInfo.getCity());
        room.setContainer(roomInfo.getContainer());
        room.setDetail(roomInfo.getDetail());
        room.setRoom_num(roomInfo.getRoom_num());
        room.setTitle(roomInfo.getTitle());
        room.setType(roomInfo.getType());
        room.setUserId(roomInfo.getUserId());
        room.setFirst_image(roomInfo.getAvatar());
        room.setPrice(roomInfo.getPrice());
        roomDao.insert(room);
        List<String> images = roomInfo.getImages();
        for (String image : images) {
            ImageEntity imageEntity = new ImageEntity();
            imageEntity.setImgUrl(image);
            imageEntity.setRoomId(room.getId());
            imageDao.insert(imageEntity);
        }
        return R.ok();
    }

    @Override
    public R searchRoomByUser(Long id) {
        List<RoomEntity> roomList = roomDao.selectList(new QueryWrapper<RoomEntity>().eq("userId", id));
        return R.ok().put("data",roomList);
    }

    @Override
    public R recommendRoom(String location, Integer pageNum, Integer pageSize,Long userId) {
        Page<RoomEntity> page = new Page<>(pageNum, pageSize);
        Page<RoomEntity> rooms = roomDao.selectPage(page, new QueryWrapper<RoomEntity>().like("address", location)
                .eq("status",1).ne("userId",userId));
        return R.ok().put("data",rooms);
    }

    @Override
    public R setPriceById(Long id, BigDecimal price) {
        RoomEntity roomEntity = roomDao.selectById(id);
        roomEntity.setPrice(price);
        roomEntity.setStatus(2);
        roomDao.updateById(roomEntity);
        return R.ok();
    }


}